package org.lease.web.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.lease.common.result.Result;
import org.lease.model.dto.LeaseAgreementDTO;
import org.lease.model.entity.LeaseAgreement;
import org.lease.model.enums.LeaseStatus;
import org.lease.model.query.LeaseAgreementPageQuery;
import org.lease.service.LeaseAgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "租约管理")
@RestController
@RequestMapping("/admin/agreement")
public class LeaseAgreementController {
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Operation(summary = "分页条件查询租约信息")
    @GetMapping("/page")
    public Result<IPage<LeaseAgreementDTO>> pageQuery(LeaseAgreementPageQuery query) {
        IPage<LeaseAgreementDTO> page = leaseAgreementService.pageQueryLeaseAgreementDTO(query);
        return Result.success(page);
    }

    @Operation(summary = "根据 ID 查询租约详细信息")
    @GetMapping
    public Result<LeaseAgreementDTO> getAgreementById(Long id) {
        LeaseAgreementDTO apartment = leaseAgreementService.getLeaseAgreementDTOById(id);
        return Result.success(apartment);
    }

    @Operation(summary = "保存或更新租约信息")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody LeaseAgreement agreement) {
        leaseAgreementService.saveOrUpdate(agreement);
        return Result.success();
    }

    @Operation(summary = "根据 ID 删除租约信息")
    @DeleteMapping("/removeById")
    public Result removeById(@RequestParam Long id) {
        leaseAgreementService.removeById(id);
        return Result.success();
    }

    @Operation(summary = "根据 ID 更新租约状态")
    @PostMapping("/updateStatusById")
    public Result updateStatusById(@RequestParam Long id, @RequestParam LeaseStatus status) {
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(LeaseAgreement::getId, id);
        updateWrapper.set(LeaseAgreement::getStatus, status);
        leaseAgreementService.update(updateWrapper);
        return Result.success();
    }
}
